<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class CommonCodeApi extends Controller
{
     //生成验证码
  public function buildCode(Request $request)
  {
    $builder = new CaptchaBuilder;
    $builder->build();

    //获取验证码内容
    $phrase = $builder->getPhrase();

    //将验证码存到session
     $request->session()->put('code', $phrase);

    //生成图片
    header("Cache-Control: no-cache, must-revalidate");
    header('Content-Type: image/jpeg');
    $builder->output();
  }

  //验证码验证
  public  static function CheckCode (Request $request, $formCodeName = 'code')
  {
       $userCode = $request->input($formCodeName);
       if ( $request->session()->get('code') == $userCode ) {
            return   true;
       }
            return false;
  }
}
